//链表 - k个一组翻转链表
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        //先求出需要翻转多少组
        ListNode* cur = head;
        int cnt = 0;
        while(cur)
        {
            cnt++;
            cur = cur->next;
        }
        cnt /= k;
        //循环cnt组, 每组进行k个链表的翻转(头插)
        ListNode* newHead = new ListNode(-1), *prev = newHead;
        newHead->next = head; 
        cur = newHead->next;
        for(int i = 1; i <= cnt; i++)
        {
            ListNode* tmp = cur;
            for(int j = 1; j <= k; j++) //头插
            {
                ListNode* next = cur->next;
                cur->next = prev->next;
                prev->next = cur;
                cur = next;
            }
            prev = tmp;
        }
        prev->next = cur;
        cur = newHead->next;
        delete newHead;
        return cur;
    }
};